package com.nineclock;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import com.nineclock.listener.EmpParseListener;
import com.nineclock.pojo.Emp;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
public class Demo {

    public static void main(String[] args) {

        write();
    }

    public static void write(){

        WriteCellStyle cellStyle = new WriteCellStyle();
        //表头和内容都是水平居中
        cellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        //设置字体
        WriteFont font = new WriteFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)12);
        cellStyle.setWriteFont(font);

        HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(cellStyle,cellStyle);

        EasyExcel.write(new File("C:\\Users\\sun\\Desktop\\test1.xlsx"))
                .head(handlerHead())
                .registerWriteHandler(styleStrategy)
                .registerWriteHandler(new MyColumnStyle())
                .sheet("测试数据")
                .doWrite(handlerData());
    }

    private static List<List<String>> handlerData() {
        List<List<String>> list = new ArrayList<>();
        list.add(Arrays.asList("10001", "亮哥", "18", "男", "小兵"));
        list.add(Arrays.asList("10002", "响哥", "38", "男", "领导"));
        list.add(Arrays.asList("10003", "福哥", "38", "男", "领导"));
        list.add(Arrays.asList("10004", "松哥", "40", "男", "大领导"));
        return  list;
    }

    private static List<List<String>> handlerHead() {
        List<List<String>> list = new ArrayList<>();
        list.add(Arrays.asList("编号"));
        list.add(Arrays.asList("姓名"));
        list.add(Arrays.asList("年龄"));
        list.add(Arrays.asList("性别"));
        list.add(Arrays.asList("备注"));
        return list;
    }

    public static void read(){
        /**
         * 参数1： 文件， 流
         * 参数2： class对象（映射excel表头的模型）
         * 参数3： 解析excel监听器
         */
        EasyExcel.read(
                new File("C:\\Users\\sun\\Desktop\\test.xlsx"), Emp.class, new EmpParseListener())
                .sheet(0) //读取 第一个工作表
                .doRead(); //执行读取

    }

    static class MyColumnStyle extends AbstractColumnWidthStyleStrategy {
        protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList,
                                      Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
            writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), 4500);
        }
    }
}
